package com.zaofan.controller;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zaofan.bean.ProductClass;
import com.zaofan.bean.ShopCategory;
import com.zaofan.dao.ProductTypeDao;
import com.zaofan.dao.ShopDao;
import com.zaofan.service.ShopCategoryService;
import com.zaofan.service.ShopService;
import com.zaofan.utils.JsonUtils;
import com.zaofan.utils.WebUtils;

@WebServlet("/category")
public class ShopCategoryController extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private ShopCategoryService shopCategoryService = new ShopCategoryService();
	private ShopService shopService = new ShopService();

	protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Map<String, Object> params = new HashMap<>();
			Enumeration<String> enumeration = request.getParameterNames();
			while (enumeration.hasMoreElements()) {
				String name = enumeration.nextElement();
				if ("action".equals(name)) {
					continue;
				}
				Object value = request.getParameter(name);
				params.put(name, value);
			}
			List<ShopCategory> shopCategorys = shopCategoryService.findAll(params);
			Long count = shopCategoryService.count(params);
			JsonUtils.writeSuccessPageJson("查询成功", shopCategorys, count, response);

		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("参数错误!!", response);
		}
		JsonUtils.writeListFailJson("查询失败!!", response);
	}

	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			ShopCategory shopCategory = WebUtils.write2bean(request, ShopCategory.class);
			shopCategoryService.add(shopCategory);
			JsonUtils.writeSuccessJson("添加成功", "", response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("添加失败!!" + e.getMessage(), response);
		}
		JsonUtils.writeListFailJson("添加失败!!", response);
	}

	protected void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			ShopCategory shopCategory = WebUtils.write2bean(request, ShopCategory.class);
			shopCategoryService.update(shopCategory);
			JsonUtils.writeSuccessJson("修改成功", "", response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeFailJson("修改失败!!" + e.getMessage(), response);
		}
		JsonUtils.writeListFailJson("修改失败!!", response);
	}

	protected void get(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String id_s = request.getParameter("id");
			Integer id = Integer.valueOf(id_s);
			ShopCategory shopCategory = shopCategoryService.findById(id);
			JsonUtils.writeSuccessJson("查询成功", shopCategory, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询成功!!" + e.getMessage(), response);
		}
		JsonUtils.writeBeanFailJson("查询成功!!", response);
	}

	protected void shop_category(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String id_s = request.getParameter("shopId");
			Integer shopId = Integer.valueOf(id_s);
			List<ShopCategory> shopCategory = shopCategoryService.findByShopId(shopId);
			JsonUtils.writeSuccessJson("查询成功", shopCategory, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询成功!!" + e.getMessage(), response);
		}
		JsonUtils.writeBeanFailJson("查询成功!!", response);
	}

	protected void productType(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String id_s = request.getParameter("shopId");
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("cityId", shopService.findById(Integer.valueOf(id_s)).getCityId());
            ProductTypeDao dao = new ProductTypeDao();
            List<ProductClass>  producttypes = dao.findAll(params);
			JsonUtils.writeSuccessJson("查询成功", producttypes, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("查询成功!!" + e.getMessage(), response);
		}
		JsonUtils.writeBeanFailJson("查询成功!!", response);
	}

}
